<template>
  <div class="page-container">
    <div class="in-container">
      <AppSearchForm @submit="search" @reset="reset">
        <el-form-item>
          <el-input
            v-model="query.name"
            clearable
            placeholder="输入内容模糊搜索"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="query.date"
            type="daterange"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
      </AppSearchForm>
      <AppPageTable
        stripe
        :data="tableData.data"
        @change="getList"
        :autostyle="'height: calc(100% - 41px);'"
        :option="tableData.option"
        class="table"
      >
        <el-table-column type="expand">
          <template #default="scope">
            <span>请求参数：{{ scope.row.requestParameters }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createBy" label="用户名" />
        <el-table-column prop="requestIp" label="IP" />
        <el-table-column
          :show-overflow-tooltip="true"
          prop="ipAddress"
          label="IP来源"
        />
        <el-table-column
          prop="description"
          label="描述"
          show-overflow-tooltip
        />
        <el-table-column prop="operatingSystem" label="操作系统" />
        <el-table-column prop="deviceType" label="设备类型" />
        <el-table-column prop="browserName" label="浏览器" />
        <el-table-column prop="version" label="版本号" />
        <el-table-column prop="area" label="区域" />
        <el-table-column prop="controller" label="控制器" width="180px" />
        <el-table-column prop="action" label="方法名称" />
        <el-table-column prop="method" label="请求类型" />
        <el-table-column prop="createTime" label="创建日期" width="140px">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="异常详情" width="100px">
          <template #default="scope">
            <el-button size="small" link @click="getDetail(scope.row.id)"
              >查看详情</el-button
            >
          </template>
        </el-table-column>
      </AppPageTable>
      <el-dialog v-model="dialogData.show" title="异常详情" width="1000px">
        <div
          v-loading="dialogData.loading"
          style="width: 100%; max-height: 500px; overflow: auto"
        >
          <highlightjs language="java" :code="errorlog" />
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { defineComponent } from "vue";
import AppSearchForm from "@/components/AppSearchForm.vue";
import AppPageTable, { useTableOption } from "@/components/AppPageTable2";
import AppTitleBox from "@/components/AppTitleBox.vue";
import "highlight.js/lib/common";
import hljsVuePlugin from "@highlightjs/vue-plugin";
import dayjs from "dayjs";
export default defineComponent({
  components: {
    AppSearchForm,
    AppPageTable,
    AppTitleBox,
    highlightjs: hljsVuePlugin.component,
  },
  data() {
    return {
      tableData: {
        data: [],
        option: useTableOption(),
      },
      query: {
        name: "",
        date: [],
      },
      dialogData: {
        loading: false,
        show: false,
      },
      errorlog: "",
    };
  },
  methods: {
    parseTime(data) {
      return dayjs(data).format("YYYY-MM-DD HH:mm:ss");
    },
    parseCode(data) {
      return data.replace(/(?:\r\n|\r|\n)/g, "<br/>");
    },
    search() {
      this.tableData.option.page = 1;
      this.getList();
    },
    reset() {
      this.query = {
        name: "",
        date: [],
      };
      this.tableData.option.page = 1;
      this.getList();
    },
    getList() {
      let url = "/api/exception/query";
      if (this.query.date && this.query.date.length > 0) {
        this.query.date[0] = dayjs(this.query.date[0]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
        this.query.date[1] = dayjs(this.query.date[1])
          .add(1, "day")
          .subtract(1, "second")
          .format("YYYY-MM-DD HH:mm:ss");
        url =
          url +
          `?CreateTime=${this.query.date[0]}&CreateTime=${this.query.date[1]}`;
      }
      this.tableData.option.loading = true;
      this.$http
        .get(url, {
          PageIndex: this.tableData.option.page,
          PageSize: this.tableData.option.pageSize,
          KeyWords: this.query.name,
        })
        .then((res) => {
          this.tableData.data = res.content;
          this.tableData.option.total = res.totalElements;
        })
        .catch((err) => {
          this.$message.error("表格数据请求失败");
        })
        .finally(() => {
          this.tableData.option.loading = false;
        });
    },
    getDetail(id) {
      this.dialogData.show = true;
      this.dialogData.loading = true;
      this.$http.get("/api/exception/detail/" + id).then((res) => {
        this.errorlog = res.exceptionInfoFull;
        this.dialogData.loading = false;
      });
    },
  },
  created() {
    this.getList();
  },
});
</script>

<style lang="scss" scoped></style>
